package com.ruoyi.standingbook.v2toilet.report;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.enums.WriteDirectionEnum;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.excel.write.metadata.fill.FillConfig;
import com.alibaba.excel.write.metadata.fill.FillWrapper;
import com.ruoyi.stat.toilet.domain.vo.CommunityToiletStatVo;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class DayReportUtil {
    static List<String> unSet = Arrays.asList("status2", "status1", "list", "max", "min", "columns");
    //横向填充
    static FillConfig horizontal = FillConfig.builder().direction(WriteDirectionEnum.HORIZONTAL).build();
    //纵向填充
    static FillConfig vertical = FillConfig.builder().direction(WriteDirectionEnum.VERTICAL).build();

    public static void listInfo(JSONObject list, ExcelWriter writer, WriteSheet sheet, JSONObject itemData) {
        // 清洁频率
        //JSONObject list = communityToiletStatLogic.getCommunityToiletStatDateAndMaxMin(streetId, "1", new Date());
        list.forEach((k, v) -> {
            if (!unSet.contains(k)) {
                itemData.set("list_".concat(k), v);
            }
        });
        list.getJSONObject("status2").forEach((k, v) -> itemData.set("list_status2_".concat(k), v));
        list.getJSONObject("status1").forEach((k, v) -> itemData.set("list_status1_".concat(k), v));
        list.getJSONObject("columns").forEach((k, v) -> itemData.set(k.concat("_head"), v));
        List<Object> rowData = new ArrayList<>();
        list.getJSONArray("list").forEach(e -> {
            JSONObject jsonObject = JSONUtil.parseObj(e);
            rowData.add(jsonObject);
        });

        //        log.info(JSONUtil.toJsonStr(list.get("list")));
        //        List<V2ToiletVo> toiletVos = list.getBeanList("list", V2ToiletVo.class);
        //        log.info(" list \n{}", JSONUtil.toJsonStr(toiletVos.get(0)));
        //        writer.fill(new FillWrapper("list", toiletVos), sheet);//horizontal,
        //        List<V2ToiletVo> toiletVosCount = toiletVos.stream().filter(e -> e.getIdx() < 0).collect(Collectors.toList());
        //        log.info(" list count\n{}", JSONUtil.toJsonStr(toiletVosCount));
        writer.fill(new FillWrapper("list", rowData), sheet);
    }


    public void countDay(JSONObject dayObject, ExcelWriter writer, WriteSheet sheet, JSONObject itemData) {

        log.info("day\r\n{}", dayObject);
        dayObject.getJSONObject("status2").forEach((k, v) -> itemData.set("day_status2_".concat(k), v));
        dayObject.getJSONObject("status1").forEach((k, v) -> itemData.set("day_status1_".concat(k), v));

        dayObject.forEach((k, v) -> {
            if (!unSet.contains(k))
                itemData.set("day_".concat(k), v);
        });
    }

    public static void countMonth(JSONObject month, ExcelWriter writer, WriteSheet sheet, JSONObject itemData) {

        log.info("month\r\n{}", month);
        month.getJSONObject("status2").forEach((k, v) -> itemData.set("month_status2_".concat(k), v));
        month.getJSONObject("status1").forEach((k, v) -> itemData.set("month_status1_".concat(k), v));
        month.forEach((k, v) -> {
            if (!unSet.contains(k))
                itemData.set("month_".concat(k), v);
        });
    }

    /**
     * 获取街道或社区 公厕数量
     */
    public static void toiletCountData(List<CommunityToiletStatVo> toiletCount, ExcelWriter writer, WriteSheet sheet, JSONObject itemData) {

        int sumCount = toiletCount.stream().mapToInt(CommunityToiletStatVo::getToiletNum).sum();
        CommunityToiletStatVo sumVo = new CommunityToiletStatVo();
        sumVo.setToiletNum(sumCount);
        sumVo.setCommunityName("合计");
        toiletCount.add(sumVo);

        writer.fill(new FillWrapper("tc", toiletCount), horizontal, sheet);

    }

}
